<?php
use app\common\model\Category;
use fast\Form;
use fast\Http;
use fast\Tree;
use think\Db;
use think\Config;
use think\Cache;

if (!function_exists('code2session')) {

    /**
     * @param string  $cdoe
     * @return mixed
     */
    function code2session($code){
        $params=[
            'appid'=>Config::get('wechat.app_id'),
            'secret'=>Config::get('wechat.app_secret'),
            'js_code'=>$code,
            'grant_type'=>'authorization_code'
        ];
        $str=http_build_query($params);
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL, 'https://api.weixin.qq.com/sns/jscode2session?'.$str);//抓取指定网页
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//要求结果为字符串且输出到屏幕上
        $data = curl_exec($ch);//运行curl
        curl_close($ch);
        if(!$data){
            return false;
        }else{
            $data=json_decode($data,true);
        }
        if(isset($data['errcode']) && $data['errcode']!=0){
            $error=[-1=>'系统繁忙，此时请开发者稍候再试',40029=>'code 无效',45001=>'频率限制，每个用户每分钟100次'];
            echo json_encode($error);
            exit();
        }
        return $data;
    }
}

if(!function_exists('mobileEncrypt')){
    /**
     * 手机号脱敏
     * @param string $mobile
     * @return string $mobile
     */
    function mobileEncrypt($mobile){
        return substr_replace($mobile, '****', 3, 4);
    }
}

if (!function_exists('getAge')) {
    /**
     * 获取年龄区间
     */
    function getAge($id){
        $ages['25以下']=['<',25];
        $ages['25-30']=['between',['25','30']];
        $ages['30-35']=['between',['30','35']];
        $ages['35-40']=['between',['35','40']];
        $ages['40-45']=['between',['40','45']];
        $ages['45-50']=['between',['45','50']];
        $ages['50以上']=['>',50];
        return $ages[$id];
    }
}
if (!function_exists('getEreaName')) {
    /**
     * 获取地区
     */
    function getEreaName($id){
        $row=Db::table('fr_area')->field('id,name')->cache(86400)->where(['id'=>$id])->find();
        return $row['name'];
    }
}
if (!function_exists('createCode')) {
    /**
     * 邀请码生成
     * @param $user_id
     * @return string
     */
    function createCode($user_id)
    {
        static $source_string = 'E5FCDG3HQA4A1NOPIJ2RSTUV67MWX89KOYZ';
        $num = $user_id;
        $code = '';
        while ($num > 0) {
            $mod = $num % 35;
            $num = ($num - $mod) / 35;
            $code = $source_string[$mod] . $code;
        }
        if (empty($code[3])){
            $code = str_pad($code, 4, '0', STR_PAD_LEFT);
        }
        return $code;
    }
}

if (!function_exists('decode')) {
    /**
     * 邀请码解码
     * @param $code
     * @return float|int
     */
    function decode($code)
    {
        static $source_string = 'E5FCDG3HQA4A1NOPIJ2RSTUV67MWX89KOYZ';
        if (strrpos($code, '0') !== false){
            $code = substr($code, strrpos($code, '0') + 1);
        }
        $len = strlen($code);
        $code = strrev($code);
        $num = 0;
        for ($i = 0; $i < $len; $i++) {
            $num += strpos($source_string, $code[$i]) * pow(35, $i);
        }
        return $num;

    }
}



if(!function_exists('curlWxImgCheck')){
    function curlWxImgCheck($access_token,$params_string){
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL, 'https://api.weixin.qq.com/wxa/servicemarket?access_token='.$access_token);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
                'Content-Type: application/json',
                'Content-Length: ' . strlen($params_string)]
        );
        curl_setopt($ch, CURLOPT_POSTFIELDS,$params_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//要求结果为字符串且输出到屏幕上
        $data = curl_exec($ch);//运行curl
        curl_close($ch);
        return $data;
    }
}
if(!function_exists('curlWxMsgCheck')){
    function curlWxMsgCheck($access_token,$params_string){
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL, 'https://api.weixin.qq.com/wxa/msg_sec_check?access_token='.$access_token);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
                'Content-Type: application/json',
                'Content-Length: ' . strlen($params_string)]
        );
        curl_setopt($ch, CURLOPT_POSTFIELDS,$params_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//要求结果为字符串且输出到屏幕上
        $data = curl_exec($ch);//运行curl
        curl_close($ch);
        return $data;
    }
}
if (!function_exists('howOld')){
    /**
     * 根据时间戳计算年龄
     * @param $birth
     * @return mixed
     */
    function howOld($birth) {
        list($birthYear, $birthMonth, $birthDay) = explode('-', date('Y-m-d', $birth));
        list($currentYear, $currentMonth, $currentDay) = explode('-', date('Y-m-d'));
        $age = $currentYear - $birthYear - 1;
        if($currentMonth > $birthMonth || $currentMonth == $birthMonth && $currentDay >= $birthDay)
            $age++;

        return $age;
    }
}
